/*
 * 保存部门信息交易
 * 参数   交易码：trancode=14123
 * 
 */

package org.ccams.tran;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.ccams.domain.TDept;
import org.json.JSONArray;
/**
 * 保存部门信息交易
 * @author liuguangshuai@gmail.com
 * @系统名称：定制化辅助生成制造系统
 * @时间：Sep 18, 2008
 */
public class Tran14123 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String params = (String) request.getParameter("Dept");
		JSONArray Depts = new JSONArray(params);
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";
		JSONArray newDepts = new JSONArray();
		int k = 0;
		for(int i=0;i<Depts.length();i++){
			org.json.JSONObject dept = Depts.optJSONObject(i);
			if(dept.has("idtDept")){
				TDept t = new TDept();
				t.setValid(0);
				t.setDeptAddress(dept.getString("deptAddress"));
				t.setDeptName(dept.getString("deptName"));
				t.setSmallName(dept.getString("smallName"));
				t.setMainPid(dept.getLong("mainPid"));
				t.setIdtDept(dept.getLong("idtDept"));
				k = getCcamsService().getTdeptDao().updateByPrimaryKeySelective(t);	//更新			
			}else{
				newDepts.put(dept);
			}			
		}
		for(int i=0;i<newDepts.length();i++){
			org.json.JSONObject newDept = Depts.optJSONObject(i);
			TDept t = new TDept();
			t.setValid(0);
			t.setDeptAddress(newDept.getString("deptAddress"));
			t.setDeptName(newDept.getString("deptName"));
			t.setSmallName(newDept.getString("smallName"));
			t.setMainPid(newDept.getLong("mainPid"));
			k = getCcamsService().getTdeptDao().insert(t);//插入
		}
		if (k == 1) {
			success = true;
			message = "部门保存成功";
		} else {
			success = false;
			message = "部门保存失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
